'use strict';

const { dialog } = require('electron');
const { BrowserWindow } = require('electron');
const { logger } = require('ee-core/log');
const path = require('path');
const fs = require('fs')

/**
 * 音频文件控制器
 * @class
 */
class AudioController {

  /**
   * 选择音频文件
   * @param args 前端传的参数
   * @param event - ipc通信时才有值
   */
  async selectFiles(args, event) {
    try {
      // 获取当前窗口
      const win = BrowserWindow.getFocusedWindow();
      
      // 打开文件选择对话框
      const result = await dialog.showOpenDialog(win, {
        title: '选择音频文件',
        filters: [
          { name: '音频文件', extensions: ['mp3', 'wav', 'ogg', 'flac'] }
        ],
        properties: ['openFile', 'multiSelections']
      });

      if (result.canceled) {
        return { status: 'canceled' };
      }

      // 处理选择的文件
      const files = result.filePaths.map(filePath => {
        return {
          path: filePath,
          name: path.basename(filePath),
          size: fs.statSync(filePath).size, // 获取文件大小（字节）
          type: path.extname(filePath).substring(1)
        };
      });

      logger.info('选择的音频文件:', files);

      return {
        status: 'ok',
        data: files
      };
    } catch (error) {
      logger.error('选择音频文件失败:', error);
      return {
        status: 'error',
        message: error.message
      };
    }
  }
}

AudioController.toString = () => '[class AudioController]';

module.exports = AudioController;